package com.xd.study.activity;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.widget.SimpleAdapter;

import com.xd.study.R;
import com.xd.study.widget.UpLoadListView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by DGF on 2015/5/12.
 */
public class ListViewUpLoadActivity extends Activity {

    private List<Map<String, Object>> data;
    private SimpleAdapter adapter;
    private UpLoadListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listview_up_load);

        data = new ArrayList<Map<String,Object>>();
        for (int i=0;i<10;i++){
            Map<String,Object> map = new HashMap<String, Object>();
            map.put("pic", R.drawable.toggle_button_off);
            map.put("txt", "慕课网"+i);
            map.put("info", "详细信息详细信息详细信息");
            data.add(map);
        }
        adapter = new SimpleAdapter(this, data,
                R.layout.listview_item,
                new String[]{"pic","txt", "info"},
                new int[]{R.id.item_image, R.id.item_text, R.id.item_info});

        listView = (UpLoadListView)findViewById(R.id.up_load_listview);
        listView.setAdapter(adapter);

        listView.setLoadListener(new UpLoadListView.ILoadListener() {
            @Override
            public void onLoad() {

                Handler handler = new Handler();
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        /**
                         * 获取更多数据
                         */
                        for (int i=0;i<2;i++){
                            Map<String,Object> map = new HashMap<String, Object>();
                            map.put("pic", R.drawable.toggle_button_off);
                            map.put("txt", "更多数据"+i);
                            map.put("info", "详细信息详细信息详细信息");
                            data.add(map);
                        }

                        /**
                         * 通知数据更新
                         */
                        adapter.notifyDataSetChanged();

                        /**
                         * 通知加载完毕
                         */
                        listView.loadCompleted();
                    }
                }, 2000);
            }
        });
    }
}
